<html>
<head><meta charset="utf-8"><title>Implementation plan for scope api · wg-async-foundations · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/index.html">wg-async-foundations</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html">Implementation plan for scope api</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="244890859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244890859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244890859">(Jul 05 2021 at 03:31)</a>:</h4>
<p>Is there anywhere I can read about how the scoped-tasks api's will be implemented (not necessarily the hookings into std, but how to deal with the limitations of either 1. unsafe or 2. blocking that <a href="https://docs.rs/async-scoped/0.6.1/async_scoped/">https://docs.rs/async-scoped/0.6.1/async_scoped/</a> has)? A lot of my work teaching people how to do concurrency and blocking concurrency, as well as work on <code>impedance</code> would be easier or very nicely invalidated with the scope apis, and I am wondering if there is a way for me to help on that part of the vision</p>



<a name="244890865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244890865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244890865">(Jul 05 2021 at 03:32)</a>:</h4>
<p>Is it blocked on <code>AsyncDrop</code>?</p>



<a name="244892647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244892647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244892647">(Jul 05 2021 at 04:18)</a>:</h4>
<p>There is no plan (in terms of a design doc) yet</p>



<a name="244892773"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244892773" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244892773">(Jul 05 2021 at 04:21)</a>:</h4>
<p><a href="https://carllerche.com/2021/06/17/six-ways-to-make-async-rust-easier/">https://carllerche.com/2021/06/17/six-ways-to-make-async-rust-easier/</a> lists one way with <code>scope</code> and <code>TaskSet</code>, which looks very reasonable (and is pretty much what Swift does).</p>
<p><a href="https://github.com/tokio-rs/tokio/issues/1879">https://github.com/tokio-rs/tokio/issues/1879</a> and <a href="https://github.com/tokio-rs/tokio/pull/2579">https://github.com/tokio-rs/tokio/pull/2579</a> described and implemented also something similar - but without the support for completion futures it had some sharp edges on its own.</p>



<a name="244898414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244898414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244898414">(Jul 05 2021 at 06:24)</a>:</h4>
<p>How does completion futures interact with this to make it less sharp?</p>



<a name="244903718"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244903718" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Alice Ryhl <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244903718">(Jul 05 2021 at 07:41)</a>:</h4>
<p>Without completion futures, spawned tasks must still be <code>'static</code> even if you're using a scope. The scope only lets you control termination of the tasks, not borrowing data across tasks.</p>



<a name="244969372"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244969372" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244969372">(Jul 05 2021 at 19:57)</a>:</h4>
<p>Forgive me for my ignorance, but how do completion futures let you use borrowed data? Is that requiring the object returned by the scope call is a completion future? or requiring that the spawned futures are completion futures?</p>
<p>Also, if we had AsyncDrop, would it be easier to call poll on a completion future without needing unsafe? (because you can use an asyncdrop impl to finish the polling)?</p>



<a name="244973014"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244973014" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kestrer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244973014">(Jul 05 2021 at 20:47)</a>:</h4>
<p>It works because the future returned by <code>scope</code> is a completion future. Spawned futures can be either, but will probably be completion futures because that's more general. It has to be a completion future to guarantee that the data it references isn't destroyed before all the running tasks that reference it complete.<br>
I don't think AsyncDrop would help, because it won't be guaranteed to be called.</p>



<a name="244984052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244984052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244984052">(Jul 06 2021 at 00:29)</a>:</h4>
<p>How will the scope's completion future be polled and guaranteed to be polled before drop (without making users use <code>unsafe</code>)?</p>



<a name="244993587"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244993587" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244993587">(Jul 06 2021 at 04:02)</a>:</h4>
<p><code>.await</code> (or as proposed just a calling of an async function) would drive the completion future to completion. Users don’t have to use unsafe things.  And the type system will make sure you can’t work around that contract without using unsafe. Eg  you can‘t <code>select!</code> on those are using other mean which poll the Future just once and then leave it alone.</p>



<a name="244993667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/244993667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#244993667">(Jul 06 2021 at 04:04)</a>:</h4>
<p>why it helps: there is no safe way to drop the scope Future anymore before the spawned Futures complete. And this can be enforced via a lexical scope just like with crossbeam scoped threads.</p>



<a name="245067415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/245067415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#245067415">(Jul 06 2021 at 16:54)</a>:</h4>
<p><span class="user-mention" data-user-id="204219">@Matthias247</span> is that the idea that the only way to actually start a root <code>CompletionFuture</code> is to spawn it in a specific method for <code>CompletionFuture</code>'s on the runtime itself? I see now how once you are in the context of a <code>CompletionFuture</code> the type system ensures that everything works out, but upon reading the proposals, I think the <code>completion</code> crate's <code>block_on</code> function is the only way to actually run a<code>CompletionFuture</code> so far?</p>



<a name="245070709"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/245070709" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthias247 <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#245070709">(Jul 06 2021 at 17:20)</a>:</h4>
<p>Future executor implementations (like an imaginary tokio 2.0 or tokio 1.0 with extensions) could allow to spawn a <code>CompletionFuture</code> without having to use <code>.block_on</code>. The executor would need to be implemented in a fashion where it never drops an in-progress <code>Future</code> - like the current versions do.</p>



<a name="245071470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/245071470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#245071470">(Jul 06 2021 at 17:26)</a>:</h4>
<p>So if you are in an async context and want await a CompletionFuture, you can spawn it on the runtime, and await the JoinHandle (and if thats dropped it either runs the compfuture in the background to completion or cancels it I suppose)? I am trying to understand how the community will iteratively interact with completion future's</p>



<a name="245092369"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/245092369" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kestrer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#245092369">(Jul 06 2021 at 20:09)</a>:</h4>
<p>There is also the possibility of another abstraction: a spawned <code>CompletionFuture</code> whose handle only implements <code>CompletionFuture</code> and is lazily spawned. For that kind of task, it wouldn't even need any lifetime bounds.</p>



<a name="245092578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/245092578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Gus Wynn <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#245092578">(Jul 06 2021 at 20:11)</a>:</h4>
<p><span class="user-mention" data-user-id="360486">@Kestrer</span> that would be spawned within a scope right?</p>



<a name="245092633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/245092633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kestrer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#245092633">(Jul 06 2021 at 20:12)</a>:</h4>
<p>It doesn't have to be I don't think</p>



<a name="245092778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/187312-wg-async-foundations/topic/Implementation%20plan%20for%20scope%20api/near/245092778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Kestrer <a href="https://rust-lang.github.io/zulip_archive/stream/187312-wg-async-foundations/topic/Implementation.20plan.20for.20scope.20api.html#245092778">(Jul 06 2021 at 20:13)</a>:</h4>
<p>If that abstraction can exist, I do wonder whether scoped tasks are necessary. The eager spawning scoped tasks provide can be worked around with a <code>join_tasks</code> combinator that takes a bunch of <code>CompletionFuture</code>s, polls them all once with a no-op context and then polls each in sequence to completion (thus being much more efficient than regular <code>join</code> or <code>FuturesUnordered</code>).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>